<?php
/*
 * @Author: your name
 * @Date: 2020-12-24 16:22:44
 * @LastEditTime: 2020-12-28 10:00:07
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /special/application/adminapiv3/controller/platform_manage/account/ServerUserGroupController.php
 */

namespace app\adminapiv3\controller\platform_manage\account;


use app\adminapiv3\controller\BaseController;
use app\adminapiv3\model\ServerPermissionModel;
use app\adminapiv3\model\ServerUserGroupModel;
use think\Request;

class ServerUserGroupController extends BaseController
{
    /**
     * 列表
    */
    public function index(Request $request){
        $serach = $request->only([
            'role','description','create_time'
        ],'get');

        $model = new ServerUserGroupModel();
        $list = $model->getList($serach);
        return $this->jsonOutPut($list);
    }

    /**
     * 添加
    */
    public function add(Request $request){
        $model = new ServerUserGroupModel();
        //授权 至少勾选一个模块 模块的数量至少为1
        $param = $request->only([
            'role','description',
        ],'post');
        $this->validate($param,'ServerUserGroup.add');
        $res = $model->addGroup($param);
        return $this->jsonOutPut($res);
    }

    /**
     * 修改
    */
    public function modify(Request $request){
        $model = new ServerUserGroupModel();
        $param = $request->only([
            'id','role','description',
        ],'post');
        $this->validate($param,'ServerUserGroup.modify');
        $res = $model->modifyGroup($param);
        return $this->jsonOutPut($res);
    }

    /**
     * 删除
    */
    public function del(Request $request){
        $param = $request->only(['id'],'post');
        $this->validate($param,'ServerUserGroup.del');
        $model = new ServerUserGroupModel();
        $res = $model->delGroup($param['id']);
        return $this->jsonOutPut($res);
    }

    /**
     * 授权
    */
    public function auth(Request $request){
        //本角色信息
        $model = new ServerUserGroupModel();
        //授权信息查询
        if($request->isGet()){
            $param = $request->only(['id','dataType'],'get');
            $this->validate($param,'ServerUserGroup.auth_query');
            $param['dataType'] = isset($param['dataType']) ? boolval($param['dataType']) : false;
            $authModel = new ServerPermissionModel();
            //所有权限节点
            $data['auths'] = $authModel->getAllAuth($param['dataType']);
            $data['info'] = $model->getGroupInfo($param['id']);
            return $this->jsonOutPut($data);
        }
        //保存
        if($request->isPost()){
            $param = $request->only(['id','auth_ids'],'post');
            $this->validate($param,'ServerUserGroup.auth');
            $res = $model->auth($param);
            return $this->jsonOutPut($res);
        }
    }
}